import os
import easygui as g
import unicodecsv as ucsv
# popen返回文件对象，跟open操作一样

#y=g.diropenbox(msg=None, title=None).replace('\\','/')
s=g.buttonbox(msg='请选择要导出几层的文件夹权限列表', title=' ', choices=('1', '2', '3','4','5'))
print(s)
y=g.diropenbox(msg=None, title=None)

#print(y)
data=[['路径',"管理员","可写","只读"]]
with open('test.csv', 'wb') as f:
    w = ucsv.writer(f, encoding = 'gbk')
    w.writerows(data)

def list_rights(path0):

    
    f = os.popen(r'icacls {}'.format(path0), "r")

    d = f.read().replace('(I)','').replace('(OI)','').replace('(CI)','').replace('(IO)','').replace('                                   ','')\
    .replace('                           ','').replace('                ','').replace('BUILTIN\\','').replace('CREATOR OWNER:(F)','')\
    .replace('NT AUTHORITY\\SYSTEM:(F)','').replace('ACCOUNTS\\','').replace(' ','')
    #d[0]='hello'
    # 读文件
    #.replace(' ','\r')


    f.close()
    #print('d:',d)
    list1=d.split('\n')
    try:
        list1[0]=list1[0].split(' ')[1]
    except:
        pass
    admins=[]
    for x in list1:
        #print('***')
        if "(F)" in x:
            admins.append(x)
        #print(x)
    print('管理员:',admins)
    readonlys=[]
    for x in list1:
        #print('***')
        if "(RX)" in x or "(R)" in x:
            readonlys.append(x)
        #print(x)
    print('只读用户:',readonlys)
    writers=[]
    for x in list1:
        #print('***')
        if "(M)" in x or "(W)" in x:
            writers.append(x)
        #print(x)
    print('可写用户:',writers)
    data=[[path0,admins,writers,readonlys]]
    with open('test.csv', 'ab+') as f:
        w = ucsv.writer(f, encoding = 'gbk')
        w.writerows(data)

#a=r'\\10.102.10.13\tools\60.方冬'
m=0
def export1(y,m):
    #global m
    print('m:*************',m)
    if m<=int(s):
        for x in os.listdir(y):
            #print(x)
            if os.path.isdir(y+'\\'+x):
                print('dir:',y+'\\'+x)
                list_rights(y+'\\'+x)
                w=m
                m=m+1
                
                export1(y+'\\'+x,m)
                m=w
                
                
                
    else:
        pass
    return m
        #aa=input('aa:')

export1(y,1)
os.system('start test.csv')


